using UnityEngine;using System.Collections;

public class Button : MonoBehaviour 
{
	public bool state;
    public int m_TimePushed;
    public int m_Interval;


	// Use this for initialization
	void Start () 
    {
        m_Interval = 300;

	}
	
	// Update is called once per frame
	void Update () 
	{
        if (Time.frameCount > m_TimePushed + m_Interval)
        {
            SwitchReleased();
        }

		if(state==false)
		{
			Physics.gravity=new Vector3(0.0f,-9.72f,0.0f);
		}
		else
		{
			Physics.gravity=new Vector3(0.0f,9.72f,0.0f);
		}
	}
	
	void OnTriggerEnter(Collider element)
	{
		if((element.tag=="Char") || (element.tag=="Cube") || (element.tag=="Explosion"))
		{
            m_TimePushed = Time.frameCount;
			state=true;
			Destroy(this.gameObject.transform.GetChild(0).gameObject);
			GameObject lol= (GameObject) GameObject.Instantiate( Resources.Load("Prefabs/p_ButtonPressed"));
			
			lol.transform.parent= this.gameObject.transform;
			lol.transform.position=this.gameObject.transform.position;

			
		}
	}

    void OnTriggerStay(Collider element)
    {
        if ((element.tag == "Char") || (element.tag == "Cube") || (element.tag == "Explosion"))
        {
            m_TimePushed = Time.frameCount;
        }
    }
	
	void OnTriggerExit(Collider element)
	{
     
	}
	
	public void SwitchReleased()
	{
			state=false;
			Destroy(this.gameObject.transform.GetChild(0).gameObject);
			GameObject lol= (GameObject) GameObject.Instantiate( Resources.Load("Prefabs/p_ButtonReleased"));
			lol.transform.parent= this.gameObject.transform;
			lol.transform.position=this.gameObject.transform.position;
	}
}
